// pages/hire/hire.js
Page({
  data: {
    hireData:[],
    regionDefault: true,
    region: ['上海市', '上海市', '徐汇区'],
    typeDefault:true,
    type:['实习','校招','社招'],
    typeIndex:0,
    degreeDefault:true,
    degree:['本科及以上','硕士及以上','博士及以上'],
    degreeIndex:0,
    shixi:false,
    isFullJobDefault:true,
    isFullJob:['可转正','不可转正'],
    isFullJobIndex:0,
    resume:['接受','不接收'],
    resumeDefault:true,
    resumeIndex:0,
    oersonInfoData:'',
    education:"",
    workExp:'',
    identity:'',
    save:'',
  },
  onLoad: function (options) {
    var that=this;
    wx.getStorage({
      key:'hireData',
      success: function (res) {
        that.setData({
          hireData: res.data,
          regionDefault:false,
          companyRegion:res.data.companyRegion,
          typeIndex:res.data.type,
          degreeIndex:res.data.degree,
          resumeIndex:res.data.resume,
          typeDefault:false,
          degreeDefault:false,
          resumeDefault:false,
        })
        if (that.data.hireData.type==0) {
          that.setData({
            isFullJobIndex: res.data.type,
            isFullJobDefault:false,
          })
        }
      }
    })
  },
  onReady: function () {
  
  },
  bindTypeChange: function (e) {
    this.setData({
      typeIndex: e.detail.value,
      typeDefault: false,
    })
    if(this.data.typeIndex==0){
      this.setData({
        shixi:true
      })
    }else{
      this.setData({
        shixi: false
      })
    }
  },
  bindIsFullJobChange: function (e) {
    this.setData({
      isFullJobIndex: e.detail.value,
      isFullJobDefault: false,
    })
  },
  bindResumeChange: function (e) {
    this.setData({
      resumeIndex: e.detail.value,
      resumeDefault: false,
    })
  },
  bindDegreeChange: function (e) {
    this.setData({
      degreeIndex: e.detail.value,
      degreeDefault: false,
    })
  },
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value,
      companyRegion: e.detail.value,
      regionDefault: false,
    })
  },
  formSubmit:function(e){
    var that = this;
    this.setData({
      hireData: e.detail.value,
    })
    console.log(that.data.hireData)
    console.log(!that.data.company)
    if(!this.data.hireData.company){
      wx.showModal({
        title: '提示',
        content: '请输入公司名称'
      })
    } else if (this.data.regionDefault == true) {
        wx.showModal({
          title: '提示',
          content: '请选择/输入公司地址'
        })
      }else if(!this.data.hireData.companyEmail){
        wx.showModal({
          title: '提示',
          content: '请输入企业邮箱'
        })
    } else if (!this.data.hireData.department) {
      wx.showModal({
        title: '提示',
        content: '请输入部门名称'
      })
    } else if (!this.data.hireData.job) {
      wx.showModal({
        title: '提示',
        content: '请输入岗位名称'
      })
      console.log(this.data.hireData.degreeDefault)
    } else if (this.data.degreeDefault == true) {
      console.log("ss")
      wx.showModal({
        title: '提示',
        content: '请选择招聘对象'
      })
    } else if (this.data.typeDefault==true) {
      wx.showModal({
        title: '提示',
        content: '请选择工作类型'
      })
    } else if(this.data.shixi==true){
      if (!this.data.hireData.duration){
        wx.showModal({
          title: '提示',
          content: '请输入最短时长'
        })
      } else if (!this.data.hireData.dayInWeek){
        wx.showModal({
          title: '提示',
          content: '请输入每周出勤'
        })
      }else if(this.data.isFullJobDefault==true){
        wx.showModal({
          title: '提示',
          content: '请选择能否转正'
        })
      }
    }
    if(!this.data.hireData.salary){
      wx.showModal({
        title: '提示',
        content: '请输入薪酬'
      })
    } else if (this.data.resumeDefault == true) {
      wx.showModal({
        title: '提示',
        content: '请选择是否接收在线简历'
      })
    }else if (!this.data.hireData.description) {
      wx.showModal({
        title: '提示',
        content: '请描述职位信息'
      })
    } else if (!this.data.hireData.requirement) {
      wx.showModal({
        title: '提示',
        content: '请输入岗位要求'
      })
    }else{
      // this.data.hireData.type = this.data.type[e.detail.value.type];
      // this.data.hireData.degree = this.data.degree[e.detail.value.degree];
      // this.data.hireData.isFullJob = this.data.isFullJob[e.detail.value.isFullJob];
      wx.setStorage({
        key: 'hireData',
        data: this.data.hireData,
        success: function (res) {
          wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 2000
          })  
          that.setData({
            save:true
          })
        }
      });
    }
  },
  preview:function(){
    var that=this;
    wx.getStorage({
      key:'personInfoData',
      success:function(res){
        if(res.data.identity){
          that.setData({
            personInfoData: true,
            identity: res.data.identity
          })
          wx.getStorage({
            key: 'education',
            success: function (res) {
              that.setData({
                education: true
              })
              if (that.data.identity == '毕业生') {
                wx.getStorage({
                  key: 'workExp',
                  success: function (res) {
                    that.setData({
                      workExp: true
                    })
                    that.publish()
                  },
                  fail:function(){
                    that.fail()
                  }
                })
              }else{
                that.publish()
              }
            },
            fail: function () {
              that.fail()
            }
          });
        }
      },
      fail:function(){
        that.fail()
      }
    });
  },
  publish:function(){
    if (this.data.save == true){
      wx.showModal({
        title: '提示',
        content: '确定要发布吗',
        success: function (res) {
          if (res.confirm) {
            wx.showToast({
              title: '成功',
              icon: 'success',
              duration: 2000
            })
          } else if (res.cancel) {
          }
        },
      })
    }else{
      wx.showModal({
        title: '提示',
        content: '请先填写并保存招聘信息',
        showCancel:false,
        success: function (res){
        }
      })
    }
  },
  fail:function(){
    wx.showModal({
      title: '提示',
      content: '请先完善个人信息',
      success: function (res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '../personInfo/personInfo',
          })
        } else if (res.cancel) {
        }  
      }
    })
  }
})